var oTable;
var newCustomer;
$(document).ajaxStop($.unblockUI);
$(document).ready(function(){
    $('#getAvaiRooms').attr('disabled', 'disabled');
    $('#dateRange').val('Select the dates');
    $("#customerID").val('');
    $("#customerName").val('');
    $("#customerPhone").val('');
    $("#customerEmail").val('');
    $("#checkinDate").val('');
    $("#checkoutDate").val('');
});
$(function() {
    $('#dateRange').datepick({
        rangeSelect: true,
        showTrigger: '#calImg',
        dateFormat: 'yyyy-mm-dd',
        onClose: function(){
            $('#getAvaiRooms').removeAttr('disabled');
        }
    });
    $("#getAvaiRooms").click(function() {
        var dates = $("#dateRange").datepick('getDate');
        var value = '';
        var startDate = $.datepick.formatDate('yyyy-mm-dd',dates[0]);
        var endDate = $.datepick.formatDate('yyyy-mm-dd',dates[1]);
        for (var i = 0; i < dates.length; i++) {
            value += (i == 0 ? '' : ',') + $.datepick.formatDate(dates[i]);
        }
        $('#getValue').val(value || 'none');
        $('#checkinDate').val(startDate);
        $('#checkoutDate').val(endDate);

        $("#ArrivalDate").remove();
        $("#Departure").remove();
        $("#roomIDCombo").empty();
        $.blockUI({
            message: '<img src="images/wait20.gif" />  Processing...'
        });
        $.post('controller?ArrivalDate='+startDate+'&DepartureDate='+endDate,{
            "Action":"get_available_rooms"
        },function(){
            $('#tblRoom').remove();
            $('#availableRoom').html('<table id="tblRoom" cellpadding="0" cellspacing="0" border="0" class="display" style="font-size: 95%"></table>')
            oTable = $('#tblRoom').dataTable({
                "bJQueryUI": true,
                "bProcessing": true,
                "iDisplayLength": 7,
                "sPaginationType": "full_numbers",
                "sAjaxSource": 'controller?Action=get_available_rooms&ArrivalDate='+startDate+'&DepartureDate='+endDate,
                "aoColumns": [
                {
                    "sTitle": "ID" ,
                    "sClass": "center"
                },

                {
                    "sTitle": "Number" ,
                    "sClass": "center"
                },

                {
                    "sTitle": "Type",
                    "sClass": "center"
                },
{
                    "sTitle": "Price",
                    "sClass": "center"
                },
{
                    "sTitle": "Location",
                    "sClass": "center"
                },
                ],
                "fnRowCallback": function( nRow, aData, iDisplayIndex ) {
                    /* Bold the grade for all 'A' grade browsers */

                    $("#roomIDCombo").append('<option>'+aData[0]+'</option>');
                    return nRow;
                }

            });
        });
        ('tblRoom').slideDown("slow");
    });
});

//Validation
jQuery(function(){
    jQuery("#customerID").validate({
        expression: "if (!isNaN(VAL) && VAL) return true; else return false;",
        message: "Should be a number"
    });
    jQuery("#customerEmail").validate({
        expression: "if (VAL.match(/^[^\\W][a-zA-Z0-9\\_\\-\\.]+([a-zA-Z0-9\\_\\-\\.]+)*\\@[a-zA-Z0-9_]+(\\.[a-zA-Z0-9_]+)*\\.[a-zA-Z]{2,4}$/)) return true; else return false;",
        message: "Should be a valid email address"
    });
    jQuery("#customerPhone").validate({
        expression: "if (VAL.match(/^[0][0-9]{9}$/)) return true; else return false;",
        message: "Should be a valid Phone number"
    });
});

//Validate new customer
$(document).ready(function(){
    $('#customerID').blur(function(){
        var customerID = $('#customerID').val();
        $.post('controller?Action=get_customer&CustomerID='+customerID, function(data){
            if (data=='NewCustomer'){
                $("#newCustomer").remove();
                $("#customerName").val('');
                $("#customerPhone").val('');
                $("#customerEmail").val('');
                newCustomer = "true";
            } else {
                var customer = data.split(",");
                $("#customerName").val(customer[0]);
                $("#customerPhone").val(customer[1]);
                $("#customerEmail").val(customer[2]);
                newCustomer = "false";
            };
        });
    });

    $("#bookBtn").click(function(){
        var customerID = $('#customerID').val();
        var customerName = $('#customerName').val();
        var customerEmail = $('#customerEmail').val();
        var customerPhone = $('#customerPhone').val();
        var checkinDate = $('#checkinDate').val();
        var checkoutDate = $('#checkoutDate').val();
        var roomID = $('#roomIDCombo').val();
        $.blockUI({
            message: '<img src="images/wait20.gif" />  Processing...'
        });
        $.post('controller?Action=book_rooms&customerID=' + customerID +
            '&customerName=' +customerName +
            '&customerEmail=' +customerEmail +
            '&customerPhone=' + customerPhone+
            '&checkinDate=' +checkinDate +
            '&checkoutDate=' +checkoutDate +
            '&roomID=' +roomID +
            '&NewCustomer=' + newCustomer,function(data){
                var $dialog = $('<div></div>')
                .html('<center>' + data + '</center>')
                .dialog({
                    autoOpen: false,
                    title: 'Information',
                    buttons: {
                        "Ok": function(){
                            $(this).dialog("close");
                        }
                    }
                });
                $dialog.dialog('open');
                $('#getAvaiRooms').attr('disabled', 'disabled');
                $('#dateRange').val('Select...');
                $("#customerID").val('');
                $("#customerName").val('');
                $("#customerPhone").val('');
                $("#customerEmail").val('');
                $("#checkinDate").val('');
                $("#checkoutDate").val('');
            });
    });
});
